<?php
$sanitize_all_escapes = true;
$fake_register_globals = false;
if (!isset($_REQUEST['medico'])) 
    session_start();

$landingpage = "index.php?site=" . $_SESSION['site_id'];
if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite'])) {
    $pid = $_SESSION['pid'];
} else {
    if (!isset($_REQUEST['medico'])) {
        session_destroy();
        header('Location: ' . $landingpage . '&w');
        exit;
    } 
}
$ignoreAuth = true;
include_once('../interface/globals.php');
include_once("$srcdir/patient.inc");
include_once("$srcdir/acl.inc");
include_once("$srcdir/options.inc.php");
include_once("$srcdir/formdata.inc.php");

if (isset($_REQUEST['medico']))
    $pid = $_REQUEST['set_pid'];
$medico = "";
$medicoIni = "";
if (isset($_REQUEST['medico'])) {
    $medico = "&medico=1&set_pid=".$pid;
    $medicoIni = "?medico=1&set_pid=".$pid;
}

foreach ($_POST as $key => $val) {
    if ($val == "MM/DD/YYYY") {
        $_POST[$key] = "";
    }
}

// Update patient_data and employer_data:
//
$newdata = array();
$newdata['patient_data']['id'] = $_POST['db_id'];
$fres = sqlStatement("SELECT * FROM layout_options " .
        "WHERE form_id = 'DEM' AND uor > 0 AND field_id != '' " .
        "ORDER BY group_name, seq");
while ($frow = sqlFetchArray($fres)) {
    $data_type = $frow['data_type'];
    $field_id = $frow['field_id'];
    // $value  = '';
    $colname = $field_id;
    $table = 'patient_data';
    if (strpos($field_id, 'em_') === 0) {
        $colname = substr($field_id, 3);
        $table = 'employer_data';
    }

    // if (isset($_POST["form_$field_id"])) $value = $_POST["form_$field_id"];
    $value = get_layout_form_value($frow);

    $newdata[$table][$colname] = $value;
	}
	
	if(!empty($_POST['seguro'])){
	$newdata['patient_data']['seguro'] = $_REQUEST['seguro'];
	}
	if(!empty($_POST['ocupacion'])){
		$newdata['patient_data']['ocupacion'] = $_REQUEST['ocupacion'];
	}
		
		/*	if($_SESSION['authUserID']==1){
		echo "<pre>";
		print_r($newdata['patient_data']);
		echo "<pre>";
			}
		*/
        
updatePatientData($newdata['patient_data']);
updateEmployerData($pid, $newdata['employer_data']);

$i1dob = fixDate(formData("i1subscriber_DOB"));
$i1date = fixDate(formData("i1effective_date"), date('Y-m-d'));

newInsuranceData(
        $pid, "primary", formData("i1provider"), formData("i1policy_number"), formData("i1group_number"), formData("i1plan_name"), formData("i1subscriber_lname"), formData("i1subscriber_mname"), formData("i1subscriber_fname"), formData("form_i1subscriber_relationship"), formData("i1subscriber_ss"), $i1dob, formData("i1subscriber_street"), formData("i1subscriber_postal_code"), formData("i1subscriber_city"), formData("form_i1subscriber_state"), formData("form_i1subscriber_country"), formData("i1subscriber_phone"), formData("i1subscriber_employer"), formData("i1subscriber_employer_street"), formData("i1subscriber_employer_city"), formData("i1subscriber_employer_postal_code"), formData("form_i1subscriber_employer_state"), formData("form_i1subscriber_employer_country"), formData('i1copay'), formData('form_i1subscriber_sex'), $i1date, formData('i1accept_assignment'), formData('i1policy_type')
);

$i2dob = fixDate(formData("i2subscriber_DOB"));
$i2date = fixDate(formData("i2effective_date"), date('Y-m-d'));

newInsuranceData(
        $pid, "secondary", formData("i2provider"), formData("i2policy_number"), formData("i2group_number"), formData("i2plan_name"), formData("i2subscriber_lname"), formData("i2subscriber_mname"), formData("i2subscriber_fname"), formData("form_i2subscriber_relationship"), formData("i2subscriber_ss"), $i2dob, formData("i2subscriber_street"), formData("i2subscriber_postal_code"), formData("i2subscriber_city"), formData("form_i2subscriber_state"), formData("form_i2subscriber_country"), formData("i2subscriber_phone"), formData("i2subscriber_employer"), formData("i2subscriber_employer_street"), formData("i2subscriber_employer_city"), formData("i2subscriber_employer_postal_code"), formData("form_i2subscriber_employer_state"), formData("form_i2subscriber_employer_country"), formData('i2copay'), formData('form_i2subscriber_sex'), $i2date, formData('i2accept_assignment'), formData('i2policy_type')
);

$i3dob = fixDate(formData("i3subscriber_DOB"));
$i3date = fixDate(formData("i3effective_date"), date('Y-m-d'));

newInsuranceData(
        $pid, "tertiary", formData("i3provider"), formData("i3policy_number"), formData("i3group_number"), formData("i3plan_name"), formData("i3subscriber_lname"), formData("i3subscriber_mname"), formData("i3subscriber_fname"), formData("form_i3subscriber_relationship"), formData("i3subscriber_ss"), $i3dob, formData("i3subscriber_street"), formData("i3subscriber_postal_code"), formData("i3subscriber_city"), formData("form_i3subscriber_state"), formData("form_i3subscriber_country"), formData("i3subscriber_phone"), formData("i3subscriber_employer"), formData("i3subscriber_employer_street"), formData("i3subscriber_employer_city"), formData("i3subscriber_employer_postal_code"), formData("form_i3subscriber_employer_state"), formData("form_i3subscriber_employer_country"), formData('i3copay'), formData('form_i3subscriber_sex'), $i3date, formData('i3accept_assignment'), formData('i3policy_type')
);

//if ($GLOBALS['concurrent_layout']) {
//    include_once("demographics.php");
//} else {
//    include_once("patient_summary.php");
//}
?>
<html>
    <head>
<?php html_header_show(); ?>
        <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
        <link rel="stylesheet" href="css/nightingale.css" type="text/css"/>
        <script language="javascript">
            setTimeout(function() { parent.$.fancybox.close(); }, 1000);
        </script>
    </head>

    <body class="body_top">
    <center>
        <p class="text">
            Sus datos han sido actualizados
        </p>
        <p class="text">
            Este dialogo se cerrar&aacute; autom&aacute;ticamente...
        </p>
    </center>
</body>
</html>